﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class ViewCart : System.Web.UI.Page
{
    public long tong_tien;
    protected void Page_Load(object sender, EventArgs e)
    {
        ShoppingCart Cart = (ShoppingCart)Session["gio_hang"];
        if (Cart != null)
            tong_tien = Cart.Total;
        DataTable tem = Cart;
        ListView1.DataSource = tem;
        
        //Label tem = (Label)ListView1.Items[].FindControl("tong_tien");
        
        if (!IsPostBack)
        {
            ListView1.Items.Clear();
            ListView1.DataBind();
            //this.FillShoppingCart();
        }
    }
    protected void updating(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "update_bt")
        {
           
            ListView lv = (ListView)sender;
            int l = lv.Items.Count;
            for (int i = 0; i < l; i++)
            {
                Label a = (Label)lv.Items[i].FindControl("IDTaiLieuLabel");
                int ma = System.Int32.Parse(a.Text);
                TextBox b = (TextBox)lv.Items[i].FindControl("TextBox1");
                int SoLuong = System.Int32.Parse(b.Text);

                ShoppingCart card = (ShoppingCart)Session["gio_hang"];
                DataRow Item = card.Rows.Find(ma);
                if ((int)Item["SoLuong"] != SoLuong)
                {
                    Item["SoLuong"] = SoLuong;
                    Item["ThanhTien"] = (long)(SoLuong * (int)Item["DonGia"]);
                }
                tong_tien = card.Total;
                
                

            }
            lv.DataBind();
            Session["msg"] = "Thành công! Đã cập nhật giỏ hàng!";
            Session["msg_type"] = "success";
            ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
            master.RefreshShoppingCartInfo();
            
        }
        if (e.CommandName == "del_bt")
        {
            
            ListView lv = (ListView)sender;
            string ma = (string)e.CommandArgument;
            int id = System.Int32.Parse(ma);
            ShoppingCart card = (ShoppingCart)Session["gio_hang"];
            card.Remove(id);
            Session["gio_hang"] = card;
          
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            lv.Items.Remove(dataItem);
            lv.DataBind();
            tong_tien = card.Total;
            Session["msg"] = "Thành công! Đã xóa sản phẩm ra khỏi giỏ hàng!";
            Session["msg_type"] = "success";
            ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
            master.RefreshShoppingCartInfo();
        }
      
    }

    public string format_tt(string tt)
    {

        string rs = tt;
        //thay dau ',' thanh '.'
        int l = (rs.Length - 1) / 3;
        for (int i = 0; i < l; i++)
        {
            rs = rs.Insert(rs.Length - (i + 1) * 3 - i, ".");
        }

        return rs;
    }
}
